<template>
  <div class="potentialCustomers">
    <page-header :title="'线索(潜在客户)'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="公司名称">
              <el-input type="text" v-model="queryForm.companyName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人">
              <el-input type="text" v-model="queryForm.customerName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所有者">
              <!-- <el-input type="text" v-model="queryForm.owner"></el-input> -->
              <el-select v-model="queryForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="线索来源">
              <el-select v-model="queryForm.leadSource" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in market" :label="item.activityName" :value="item.activityName" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="线索状态">
              <el-select v-model="queryForm.leadStatus" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in lead_status" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增线索': '编辑线索'" :visible.sync="visible" @close="close" width="65%">
      <el-row>
        <el-form :form="leadForm" label-width="1.5rem">
          <el-col :span="11">
            <el-form-item label="公司名称" required>
              <el-input v-model="leadForm.companyName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="公司地址" required>
              <el-input v-model="leadForm.address" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="公司座机" required>
              <el-input v-model="leadForm.tel" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所属市场活动" required>
              <!-- <el-input v-model="leadForm.customerName" placeholder="请输入"></el-input> -->
              <el-select
                v-model="leadForm.activityIds"
                multiple
                collapse-tags
                placeholder="请选择"
                style="width: 100%" @change="activityChange">
                <el-option
                  v-for="item in market"
                  :key="item.uuid"
                  :label="item.activityName + '-' + item.ownerName"
                  :value="item.uuid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户名称" required>
              <el-input v-model="leadForm.customerName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户职位">
              <el-input v-model="leadForm.position" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户联系方式" required>
              <el-input v-model="leadForm.phone" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户邮箱">
              <el-input v-model="leadForm.mail" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户生日">
              <el-date-picker
                v-model="leadForm.birthday"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="下次联系时间">
              <el-date-picker
                v-model="leadForm.nextContact"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="联系纪要" required>
              <el-input type="textarea" v-model="leadForm.contactNotes" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="线索来源" required>
              <el-input v-model="leadForm.leadSource" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="线索状态" required>
              <el-select v-model="leadForm.leadStatus" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in lead_status" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="线索所有者" required>
              <el-select v-model="leadForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="描述">
              <el-input type="textarea" v-model="leadForm.described" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建人">
               <el-input disabled v-model="leadForm.createByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input disabled v-model="leadForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新人">
              <el-input disabled v-model="leadForm.updateByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新时间">
              <el-input disabled v-model="leadForm.updateTime"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      queryForm: {
        companyName: "",
        customerName: "",
        leadSource: "",
        leadStatus: "",
        owner: ""
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'companyName',
          label: '公司名称',
          align: 'center'
        },
        {
          prop: 'customerName',
          label: '联系人',
          align: 'center'
        },
        {
          prop: 'tel',
          label: '座机',
          align: 'center'
        },
        {
          prop: 'phone',
          label: '联系电话',
          align: 'center'
        },
        {
          prop: 'leadSource',
          label: '线索来源',
          align: 'center'
        },
        {
          prop: 'leadStatusName',
          label: '线索状态',
          align: 'center'
        },
        {
          prop: 'ownerName',
          label: '所有者',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          width: '180',
          btns: [
            {type: 'switch',desc: '转换'},
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      leadForm: {
        "activityIds": [],
        "address": "",
        "adminId": "",
        "birthday": "",
        "companyName": "",
        "contactNotes": "",
        "createBy": "",
        "createByName": "",
        "createTime": "",
        "customerName": "",
        "described": "",
        "leadSource": "",
        "leadStatus": "",
        "mail": "",
        "nextContact": "",
        "owner": "",
        "phone": "",
        "position": "",
        "tel": "",
        "updateBy": "",
        "updateByName": "",
        "updateTime": "",
      },
      total: 0,
      visible: false,
      code: '',
      userData: {},
      market: [],
      ownerList: [],
      lead_status: [],
    }
  },
  methods: {
    onHandle(data) {
      let {row,type} = data;
      if (type == 'update') {
        this.visible = true;
        this.code = '2';
        this.$post(this.$api.lead.detail,this.reqData(row.uuid)).then(res => {
          if (res.data.code == 200) {
            let data = res.data.data;
            let activityIds = data.activityList.map(item => item.uuid)
            console.log(activityIds);
            this.leadForm = data;
            this.leadForm.adminId = this.userData.uuid;
            this.leadForm.activityIds = activityIds
            console.log(this.leadForm);
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        this.switchToCustom(row.uuid)
      }
    },
    switchToCustom(leadId){
      console.log(leadId);
      this.$confirm('是否将此项线索转换为用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.$post(this.$api.lead.switch,this.reqData({
          adminId: this.userData.uuid,
          leadId
        })).then(res => {
          if(res.data.code === 200) {
            this.$message({
              type: 'success',
              message: res.data.message
            });
            this.queryList();
          } else {
             this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });          
      });
    },
    activityChange(val) {
      console.log(val);
      let result = [];
      val.forEach(ele => {
        this.market.forEach(v => {
          if (ele == v.uuid) {
            result.push(v.activityName)
          }
        })
      })
      let str = Array.from(new Set(result)).join(',')
      console.log(result,str);
      this.leadForm.leadSource = str
    },
    btnClick() {
      this.queryList()
    },
    reset() {
      this.queryForm = {
        companyName: "",
        customerName: "",
        leadSource: "",
        leadStatus: "",
        owner: ""
      }
    },
    add() {
      this.code = '1';
      this.visible = true;
      this.leadForm.adminId = this.userData.uuid;
      this.leadForm.createTime = this.$time()
      console.log(this.leadForm);
    },
    confrim() {
      let url = this.code == '1' ? this.$api.lead.create : this.$api.lead.update;
      if (this.code == '1') {
        this.leadForm.createBy = this.userData.uuid;
        this.leadForm.createByName = this.userData.userName;
      } else {
        this.leadForm.updateBy = this.userData.uuid;
        this.leadForm.updateByName = this.userData.userName;
        this.leadForm.updateTime = this.$time();
        delete this.leadForm.activityList
      }
      console.log(url,this.leadForm);
      this.$post(url,this.reqData(this.leadForm)).then(res => {
        if (res.data.code == 200) {
          this.visible = false;
          this.queryList();
          this.$message.success(res.data.message);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    close() {
      this.leadForm = {
        "activityIds": [],
        "address": "",
        "adminId": "",
        "birthday": "",
        "companyName": "",
        "contactNotes": "",
        "createBy": "",
        "createByName": "",
        "createTime": "",
        "customerName": "",
        "described": "",
        "leadSource": "",
        "leadStatus": "",
        "mail": "",
        "nextContact": "",
        "owner": "",
        "phone": "",
        "position": "",
        "tel": "",
        "updateBy": "",
        "updateByName": "",
        "updateTime": "",
      }
    },
    queryList() {
      this.$post(this.$api.lead.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1;
            item.leadStatusName = ""
            this.lead_status.forEach(v => {
              if (item.leadStatus == v.code) {
                item.leadStatusName = v.described
              }
            })
          })
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList()
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList()
    },
    marketList() {
      let url = this.$api.market.list;
      this.$post(url,this.reqData({
        activityName: '',
        owner: '',
        startDate: '',
        endDate: '',
        leadId: ''
      },1,999)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          this.market = data.list;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryUserList() {
      let url = this.$api.user.list;
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          this.ownerList = res.data.data;
        }
      })
    },
    queryCode() {
      this.$post(this.$api.common.code,this.reqData('lead_status')).then(res => {
        if (res.data.code === 200) {
          this.lead_status = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.marketList();
    this.queryUserList();
    this.queryCode();
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.queryList()
  }
}
</script>
<style scoped>
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>